﻿using System;
using System.Text.RegularExpressions;

namespace EnetaMvc.ApplicationServices.Dto
{
    public class ForumPostDto : ContentItemDto
    {
        public long ModifiedById { get; set; }
        public string ModifiedByUserName { get; set; }
        public DateTime Modified { get; set; }
        public string ModificationComment { get; set; }
        public long TopLevelPostId { get; set; }
        public int NumberOfReplies { get; set; }
        public int Views { get; set; }
        public ForumPostDto LastPost { get; set; }
        public long ForumId { get; set; }
        public string ForumName { get; set; }
        public long ForumCategoryId { get; set; }
        public string ForumCategoryName { get; set; }

        public string QuotedBody
        {
            get
            {
                var body = Body;
                var pattern = @"(\[quote\s+name=&quot;(?<user>\w+)&quot;\]|\[quote\])";
                var matches = Regex.Matches(body, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                if (matches.Count == 0)
                {
                    pattern = @"(\[quote\s+name=""(?<user>\w+)""\]|\[quote\])";
                    matches = Regex.Matches(body, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);

                    if (matches.Count == 0)
                        return Body;
                }

                foreach (Match match in matches)
                {
                    var user = string.Empty;
                    if (match.Groups["user"] != null)
                        user = match.Groups["user"].Value;

                    var replace = string.Empty;

                    if (!string.IsNullOrEmpty(user))
                    {
                        replace += "<strong class=\"blockquote\">" + user + " " + "wrote_resource" + "</strong>";
                    }

                    body = body.Replace(match.ToString(), replace + "<blockquote>");
                }
                body = body.Replace("[/quote]", "</blockquote>");
                body = body.Replace("<blockquote></p>", "<blockquote>");
                body = body.Replace("<p></blockquote>", "</blockquote>");
                body = body.Replace("<p><strong class=\"blockquote\">", "<strong class=\"blockquote\">");
                body = body.Replace("</blockquote></p>", "</blockquote>");
                body = body.Replace("<p>&nbsp;</p>", "");
                return body;
            }
        }
    }
}
